import { Component, Prefab, resources } from 'cc';
import { GetlottwardPage } from './popup.getlottwardpage';
import { NewcomerPage } from './popup.newcomerpage';
import { PasslevelPage } from './popup.passlevelpage';
import { RefreshPage } from './popup.refreshpage';
import { SettingPage } from './popup.settingpage';
import { ExceptionPage } from './popup.exceptionpage';

export enum EPopup {
    NEWCOMERPAGE,
    PASSLEVELPAGE,
    REFRESHPAGE,
    SETTINGPAGE,
    GETLOTTWARDPAGE,
    EXCEPTIONPAGE
}

const CONFIG = {
    [EPopup.NEWCOMERPAGE]: {
        component: NewcomerPage,
        path: 'prefab/pop-up/NewcomerPage',
        script: './popup.newcomerpage.ts',
        prefab: null
    },
    [EPopup.PASSLEVELPAGE]: {
        component: PasslevelPage,
        path: 'prefab/pop-up/PasslevelPage',
        script: './popup.passlevelpage.ts',
        prefab: null
    },
    [EPopup.REFRESHPAGE]: {
        component: RefreshPage,
        path: 'prefab/pop-up/RefreshPage',
        script: './popup.refreshpage.ts',
        prefab: null
    },
    [EPopup.SETTINGPAGE]: {
        component: SettingPage,
        path: 'prefab/pop-up/SettingPage',
        script: './popup.settingpage.ts',
        prefab: null
    },
    [EPopup.GETLOTTWARDPAGE]: {
        component: GetlottwardPage,
        path: 'prefab/pop-up/GetlottwardPage',
        script: './popup.getlottwardpagets',
        prefab: null
    },
    [EPopup.EXCEPTIONPAGE]: {
        component: ExceptionPage,
        path: 'prefab/pop-up/ExceptionPage',
        script: './popup.exceptionpage',
        prefab: null
    }
}

export async function GetPopPrefab(p: EPopup): Promise<Prefab> {
    let conf = CONFIG[p];
    if (!conf.prefab) {
        conf.prefab = await mtec.cc.loadResAsync(conf.path, Prefab, resources);
    }

    return conf.prefab;
}

export async function GetPopComponent<P extends EPopup>(p: P): Promise<(typeof CONFIG)[P]['component']> {
    let conf = CONFIG[p];
    if (!conf.component) {
        conf.component = await import(conf.script).then(m => m.default);
    }

    return conf.component;
}

export type PopCtor<P extends EPopup> = typeof CONFIG[P]['component'];

export type PopIns<P extends EPopup> = InstanceType<PopCtor<P>>;

export type PopParams<P extends EPopup> = Parameters<PopIns<P>['onShow']>;

export type PopReturn<P extends EPopup> = Awaited<ReturnType<PopIns<P>['show']>>;